/*
 * @Author: chenbolin@msn.cn
 * @Date: 2023-02-06 17:24:04
 * @LastEditors: chenbolin@msn.cn
 * @LastEditTime: 2023-10-28 09:48:01
 * @FilePath: \Receiver\src\Sys\Sys.h
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
#ifndef __SYS_H
#define __SYS_H

#include "Hal.h"
#include "SysDelayProcess.h"

#define SYS_PERIODIC_EXECUTION_INTERVAL 83 // 12 times in 1s
#define SYS_WIRELESS_BRAODCAST_ADDRESS 0xffffffff
#define SYS_DEVICE_MODEL_LEN 4

#define SysTime_t uint32_t
#define SysTime HalGetTicks
#define SysTimeHasPast(old, past) (SysTime() - (old) > (past))

#define sysPrint printf
#define sysLog(...)                       \
    do                                    \
    {                                     \
        sysPrint("[%s]: ", __FUNCTION__); \
        sysPrint(__VA_ARGS__);            \
        sysPrint("\r\n");                 \
    } while (0)

typedef enum
{
    SYS_DEVICE_TYPE_GKZYC = 0, // 高空作业车
    SYS_DEVICE_TYPE_DGNSCD,    // 多功能随车吊
    SYS_DEVICE_TYPE_ZBD        // 直臂吊
} SysDeviceType_t;

typedef enum
{
    SYS_ERRCODE_NONE = 0,
    SYS_ERRCODE_REMOTE_DISCONNECT
} SysErrorCode_t;

typedef enum
{
    SYS_PLATFORM_MODE_ROTATE = 0, // 旋转
    SYS_PLATFORM_MODE_LEVEL       // 平台调平
} SysPlatformMode_t;

typedef enum
{
    SYS_JOYSTICK_DIR_UP = 0,
    SYS_JOYSTICK_DIR_DOWN
} SysJoystickDirect_t;

/*比例阀*/
typedef enum
{
    SYS_VALVE_CH_DBSJ = 0, // 大臂升降
    SYS_VALVE_CH_DBSS,     // 大臂伸缩
    SYS_VALVE_CH_ZTXZ,     // 转台旋转
    SYS_VALVE_CH_DGSJ,     // 吊钩升降
    SYS_VALVE_CH_COUNT
} SysValveChannel_t;

typedef struct
{
    uint32_t wirelessAddress;
    uint8_t authCode[HAL_AUTH_KEY_LEN];
} SysManufactoryInfo_t;

void SysManufactoryInfoWrite(uint32_t wladdress, uint8_t *authcode);
SysManufactoryInfo_t *SysGetManufactoryInfo(void);
bool SysIsAuthorized(void);
bool SysAuthCodeCheck(const uint8_t *authcode);

void SysSetValveFrequency(uint16_t hz);
uint16_t SysGetPWMCount(void);
uint32_t SysWirelessAddressGet(void);

void SysThrottleReset(void);
void SysThrottleSet(uint16_t value);
const char *SysVersion(void);
const char *SysDeviceModel(void);
SysDeviceType_t SysDeviceType(void);
void SysInitialize(void);
void SysPoll(void);

#endif
